package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.PageDto;
import com.wjc.server.dto.RoleDto;
import com.wjc.server.entity.*;
import com.wjc.server.mapper.RoleMapper;
import com.wjc.server.mapper.RoleResourceMapper;
import com.wjc.server.mapper.RoleUserMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private RoleUserMapper roleUserMapper;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        RoleExample roleExample = new RoleExample();
        List<Role> roleList= roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo=new PageInfo<>(roleList);
        pageDto.setTotal(pageInfo.getTotal());

        List<RoleDto> roleDtosList = CopyUtil.copyList(roleList,RoleDto.class);
        pageDto.setList(roleDtosList);

    }

    //保存
    public void save(RoleDto roleDto){

        Role role=CopyUtil.copy(roleDto,Role.class);
        if (ObjectUtils.isEmpty(role.getId())){
            //新增
            role.setId(snowFlake.nextId());
            roleMapper.insert(role);
        }else {
            roleMapper.updateByPrimaryKey(role);
        }

    }

    /**
     * 按角色保存资源
     */
    public void saveResource(RoleDto roleDto) {
        Long roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();
        // 清空库中所有的当前角色下的记录
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(example);

        // 保存角色资源
        for (int i = 0; i < resourceIds.size(); i++) {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(snowFlake.nextId());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceIds.get(i));
            roleResourceMapper.insert(roleResource);
        }
    }

    /**
     * 按角色加载资源
     * @param roleId
     */
    public List<String> listResource(Long roleId) {
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleResource> roleResourceList = roleResourceMapper.selectByExample(example);
        List<String> resourceIdList = new ArrayList<>();
        for (int i = 0, l = roleResourceList.size(); i < l; i++) {
            resourceIdList.add(roleResourceList.get(i).getResourceId());
        }
        return resourceIdList;
    }

    /**
     * 按角色保存用户
     */
    public void saveUser(RoleDto roleDto) {
        Long roleId = roleDto.getId();
        List<String> userIdList = roleDto.getUserIds();
        // 清空库中所有的当前角色下的记录
        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleUserMapper.deleteByExample(example);

        // 保存角色用户
        for (int i = 0; i < userIdList.size(); i++) {
            RoleUser roleUser = new RoleUser();
            roleUser.setId(snowFlake.nextId());
            roleUser.setRoleId(roleId);
            roleUser.setUserId(Long.valueOf(userIdList.get(i)));
            roleUserMapper.insert(roleUser);
        }
    }

    /**
     * 按角色加载用户
     * @param roleId
     */
    public List<String> listUser(Long roleId) {
        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleUser> roleUserList = roleUserMapper.selectByExample(example);
        List<String> userIdList = new ArrayList<>();
        for (int i = 0, l = roleUserList.size(); i < l; i++) {
            userIdList.add(roleUserList.get(i).getUserId().toString());
        }
        return userIdList;
    }


    public void delete(Long id){
        roleMapper.deleteByPrimaryKey(id);
    }
}
